#include "seq.h"
#include <string>
#include <vector>
using namespace std;

seq::~seq()
{
    for(size_t i=0;i<word.size();i++)
    {
        delete word.at(i);
    }
}

regex_base* seq::copy() const
{
    seq* s = new seq;
    for(size_t i=0;i<word.size();i++)
    {
        s->word.push_back(word.at(i)->copy());
    }
    return s;
}

seq seq::operator>>(const regex_base& r) const
{
    seq new_;
    for(size_t i=0;i<word.size();i++)
    {
        new_.word.push_back(word.at(i)->copy());
    }
    new_.word.push_back(r.copy());
    return new_;
}

match_result seq::match(const std::string& s) const
{
    match_result r;
    string use = s;
    vector<string> data;

    for(size_t i=0;i<word.size();i++)
    {
        match_result x = word[i]->match(use);

        if(x.get_matched())
        {
            r = match_result(true,r.get_length()+x.get_length());
            data.push_back(string(use.begin(),use.begin()+x.get_length()));
            use = string(use.begin()+x.get_length(),use.end());
            continue;
        }
        else
        {
            return match_result();
        }
    }

    r.backref = data;
    return r;
}
